package com.py.credit.service.impl;

import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.DebugLogger;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.dao.ColumnDao;
import com.py.credit.dao.CompanyGenealogyDao;
import com.py.credit.dao.TableDao;
import com.py.credit.entity.CompanyGenealogy;
import com.py.credit.entity.GenealogyNode;
import com.py.credit.service.CompanyGenealogyService;
import com.py.framework.core.base.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业族谱服务实现类
 *
 * @author YaoS
 * @date 2019-04-15 10:17
 */
@Service("companyGenealogy")
public class CompanyGenealogyServiceImpl extends BaseServiceImpl<CompanyGenealogy, CompanyGenealogyDao> implements CompanyGenealogyService {
    @Autowired
    CompanyGenealogyDao companyGenealogyDao;
    @Autowired
    TableDao tableDao;
    @Autowired
    ColumnDao columnDao;

    @Override
    public CompanyGenealogy getDetail(String id) {
        return companyGenealogyDao.getDetail(id);
    }

    @Override
    public List<GenealogyNode> getGenealogy(String A000001) {
        List<GenealogyNode> resultList = new ArrayList<>();
        //初始化根节点
        GenealogyNode genealogyNode = new GenealogyNode();
        genealogyNode.setNodeId("0");
        genealogyNode.setNodeName(A000001);
        genealogyNode.setNodeType("100000");
        genealogyNode.setLineName("");
        genealogyNode.setLineType("");
      
        resultList.add(genealogyNode);
        //获取到关联配置list
        List<CompanyGenealogy> list = companyGenealogyDao.getAssociate();
       
        
        
        //循环根据关联配置查询关联信息
        for (CompanyGenealogy companyGenealogy : list) {
            int tableId = companyGenealogy.getId();
            String resourceId = "t_" + companyGenealogy.getResourceId();
            String associateColumn = companyGenealogy.getAssociateColumn();
            String showColumn = companyGenealogy.getShowColumn();
            //根据A000001和关联字段和获取关联表info信息
            List<GenealogyNode> nodes = companyGenealogyDao.getGenealogy(
                    A000001, resourceId, associateColumn, showColumn);
            for (GenealogyNode node : nodes) {
                node.setNodeId(tableId + "_" + node.getNodeId());
                node.setNodeType(companyGenealogy.getDataType());
                node.setLineName(companyGenealogy.getAssociateName());
                node.setLineType(companyGenealogy.getAssociateType());
               
               //从股东表,高管表查询该高管，股东，法人对应的关联信息
                List<GenealogyNode> nodeszrr = companyGenealogyDao.getGenealogyZrr(
                		node.getNodeName(), A000001, associateColumn, showColumn);
                node.setDetailData(nodeszrr);
             
                resultList.add(node);
                
            }
        }
        return resultList;
    }


    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void addCompanyGenealogy(CompanyGenealogy companyGenealogy) {
        companyGenealogyDao.addCompanyGenealogy(companyGenealogy);
    }


    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void delCompanyGenealogy(List<String> ids) {
        companyGenealogyDao.delCompanyGenealogy(ids);
    }


    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateCompanyGenealogy(CompanyGenealogy companyGenealogy) {
        companyGenealogyDao.updateCompanyGenealogy(companyGenealogy);
    }

    @Override
    public List<Map<String, Object>> getTableList() {
        return tableDao.getAllTable();
    }

	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @param name
	 * @param type
	 * @return
	 * @see com.py.credit.service.CompanyGenealogyService#getGenealogyDetail(java.lang.String, java.lang.String)
	 */
	
	@Override
	public  Map<String,Object> getGenealogyDetail(String name, String type) {
		 Map<String,Object> resultList = new HashMap<String,Object>();
		 //根据不同type到不同的table查询返回不同的数据信息		
		 String tablename = "";
		 String column="";
		 Map<String,String> baseInfo = new HashMap<>();
		try {			
		if(name!=null&&!name.equals("")) {					
		 if("GGRY".equals(type)) {			 
			 tablename="t_100000460012_9Ne2idyFF6";
			 column="ZJHM";			
			//高管人员基本信息
			 baseInfo = companyGenealogyDao.getbaseInfo(tablename,name,column);
			 //关联信息
			 List<Map<String,String>> associateInfo = companyGenealogyDao.getassociateInfo(name);
			 resultList.put("baseInfo",baseInfo);
			 resultList.put("associateInfo", associateInfo);
		 }else if("GD".endsWith(type)) {			
			 tablename = "t_100000460011_zicU3VEPuX"; 
			 column = "ZJHM";
			
			 //股东人员基本信息
			 baseInfo = companyGenealogyDao.getbaseInfo(tablename,name,column);
			 //关联信息
			 List<Map<String,String>> associateInfo = companyGenealogyDao.getassociateInfo(name);
			 resultList.put("baseInfo",baseInfo);
			 resultList.put("associateInfo", associateInfo);
		 }else {		
			 tablename="t_100000460009_JnuqJ847ju";
			 column="A000002";			
			//企业基本信息
			 baseInfo = companyGenealogyDao.getbaseInfo(tablename,name,column);
			//股东信息
			 List<Map<String,String>> gdInfo = companyGenealogyDao.getqyInfo("t_100000460011_zicU3VEPuX",name,column);
			//对外投资
			 List<Map<String,String>> otherInfo = companyGenealogyDao.getqyInfo("t_100000460011_zicU3VEPuX",name,"ZJHM");
			//高管信息
			 List<Map<String,String>>	ggInfo = companyGenealogyDao.getqyInfo("t_100000460012_9Ne2idyFF6",name,column);
			 resultList.put("baseInfo",baseInfo);
			 resultList.put("gdInfo",gdInfo);
			 resultList.put("otherInfo",otherInfo);
			 resultList.put("ggInfo",ggInfo);
		 }
		}else {
			 resultList.put("baseInfo","暂未查询到相关信息");
		}
		} catch (Exception e) {
			ExceptionLogger.error(e);
		} 
	        return resultList;
	}
}
